import axios from "axios";

// axios.defaults.method = 'POST'

const service = axios.create({
  baseURL: "/api",
  timeout: 60000,
  method: "",
});

service.interceptors.request.use(
  (config) => {
    console.log("请求拦截器", config.url, config.method);
    config.method = config.method || "post";

    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);
// 响应拦截器
service.interceptors.response.use(
  (res) => {
    const code = res.data.code;
    if (code == 0) {
      return res.data.data || {};
    } else if (code == 500) {
      return Promise.reject(new Error("接口500"));
    } else if (code == 401) {
      let envObj = import.meta.env;
      if (envObj.VITE_USER_NODE_ENV != "development") {
        window.location.href = envObj.VITE_APP_ATRUST_URL;
      }
    } else {
      return res.data || {};
    }
  },
  (error) => {
    console.log(
      "网络连接超时等异常，异常接口信息：" + JSON.stringify(error.message)
    );
    return Promise.reject(error);
  }
);
export default service;
